define(['helper/tools','pageReading','vocabularyBook'],function(tools,pageReading,vocabularyBook){
    var _this = null;
    var init = function(){
        _this = this;
        // _this.view.init();
    };
    var dataTrans = {
        getBookList: function(){
            
          tools.ajax('reader/book_list',{},
                function(res){
                    tools.__UTILS.bookList = res.book_list;
                }
            )
             // tools.__UTILS.bookList = [{'id':1,'book_name':'book1','cover':'1.jpg','introdution':'这是一本书'},{'id':2,'book_name':'book2','cover':'1.jpg','introdution':'这是一本书'},{'id':3,'book_name':'book3','cover':'1.jpg','introdution':'这是一本书'},{'id':1,'book_name':'book1','cover':'1.jpg','introdution':'这是一本书'},{'id':2,'book_name':'book2','cover':'1.jpg','introdution':'这是一本书'},{'id':3,'book_name':'book3','cover':'1.jpg','introdution':'这是一本书'}];
            
        },
        uploadFile: function(file){
            tools.tipZone("tip_right","上传中，请稍候");
            tools.ajax('import/upload',file,
                function(res){
                    if(res.status == 1){
                        tools.tipZone("tip_right",res.msg);
                    }
                 },null,
                 {
                    type:"POST",
                    contentType: false, //必须
                    processData: false,
                    dataType:"json"
                 }   
            )
        }
    };
    var view = {     
        init: function(){
            tools.router("chooseBook");
            if(tools.__UTILS.bookList.length == 0){
                _this.dataTrans.getBookList();
                _this.view.centerPos();
                $(".single_book").remove();
                $.each(tools.__UTILS.bookList,function(i,value){
                    $(".book_stack").prepend('<div class="single_book book_block" data-book_id="'+value.id
                        +'"><span class="bookshelf_bookname">'+value.book_name+'</span></div>');
                });
                _this.regBtn.singleBookClick();
               
            };
            _this.view.userWordSize();
            
            
        },
        userWordSize: function(){
            $(".chooseBook_user_word_size").html(tools.__UTILS.userActionRecord.current_vocabularySize);
        },
        centerPos: function(){
            $(".chooseBook").css({width:$(window).width()});
            $(window).resize(function(){
                $(".chooseBook").css({
                    width:'auto'
                })
            });
        }
            
    };
    var regBtn = {
        singleBookClick: function(){
            $(".single_book").click(function(){
                if(tools.__UTILS.userActionRecord.current_bookId != $(this).data("book_id")){
                    tools.__UTILS.userActionRecord.chooseBookId = $(this).data("book_id");
                    tools.__UTILS.userActionRecord.DefaultPageNumber = 1;
                    tools.__UTILS.userActionRecord.chooseBookName = $(this).find(".bookshelf_bookname").html();
                    console.log(tools.__UTILS.userActionRecord.chooseBookName);
                    pageReading.init();
                    pageReading.view.init();
                }
                else{
                    tools.router("pageReading");
                }
                tools.initiallyIntro("pageReading");
                
            });
        },
        addBookClick: function(){
            $('.add_book').bind('click',function(){
                $('.upload_book_file').click();
                $(this).hide();
                $(".upload_book_frame").show();
             });
        },
        cancelUpClick: function(){
            $(".cancelup").bind("click",function(){
                console.log("dianji cancelup");
                $("#file").after($("#file").clone().val("")); 
                $("#file").remove();
                $(".upload_book_frame").hide();
                $('.add_book').show();
             });
        },
        fileUpSubmitClick: function(){
            $('.file_up_submit_btn').click(function(){
                var temp_file= $('#file').val();
                var fileExtend=temp_file.substring(temp_file.lastIndexOf('.')).toLowerCase();
                console.log(fileExtend);
                if(fileExtend == '.epub'){
                    console.log(temp_file);
                    console.log("内容"+$('#file').get(0).files[0]);
                    var formData = new FormData($("form")[0]);
                    formData.append( 'file', $('input[name=file]')[0].files[0]);
                    console.log(formData);
                    _this.dataTrans.uploadFile(formData);
                    $(".cancelup").click();
                }
                else{
                    console.log('illegal file');
                    tools.tipZone("tip_wrong"," 请上传epub格式文件");
                    $(".cancelup").click();
                    return false;

                }
            });
        },
        init: function(){
            $(".chooseBook_vocabularyBook_btn").click(function(){
                vocabularyBook.init();
                vocabularyBook.view.init();
                tools.initiallyIntro("vocabularyBook");
            });
            _this.regBtn.addBookClick();
            _this.regBtn.cancelUpClick();
            _this.regBtn.fileUpSubmitClick();
        }     
    };
    return{
        init: init,
        view: view,
        regBtn: regBtn,
        dataTrans: dataTrans
    }

})